package dolf.objects.weapons.Mine;

import java.awt.Graphics2D;
import dolf.objects.weapons.*;
import dolf.objects.*;

import dolf.game.*;
import dolf.objects.Animation;
import dolf.objects.CircleObject;
import dolf.objects.Hole;
import dolf.objects.ImageStore;
import dolf.objects.Pit;
import dolf.objects.Point;
import dolf.objects.Vector;
import dolf.window.SystemMessagesHandler;

public class Mine extends CircleObject  implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 100;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Mine.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Mine.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Mine.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Mine/gfx/icon.png");
	
	private final int DEST_RAD=25;
	private boolean used;
	private java.util.LinkedList<String> imgVec = new java.util.LinkedList<String>();
	private Player pl;
	private Animation explosion; 
	private boolean inititalized = false;
	public Mine(){
		super(null,12,false,true);
		//animation
		for (int i = 1; i <= 23; i++) {
			imgVec.add("dolf/gfx/environment/explosion/explosion"+i+".png");
		}
		explosion = new Animation(imgVec);
	}
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.inititalized = true;

		this.used = false;
		this.pl = _pl;
		this.pl.setWeaponState();
	}
	public void deInitialize(){
		this.pl = null;
		this.inititalized = false;
	}
	
	public javax.swing.ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/Mine/gfx/icon.png");	
	}
	
	public void paint(Graphics2D g){
		g.drawImage(ImageStore.getInstance().getImage("dolf/objects/weapons/Mine/gfx/icon.png"), this.getPosition().getIntX()-this.getRadius(), this.getPosition().getIntY()-this.getRadius(), null);
	}
	
	public void execute(){
		if(!used && Level.getInstance().mousePressed()&&pl.getBall().isMoving()){
			this.setPosition(pl.getBall().getPosition().sub(pl.getBall().getMotion().setLength(this.getRadius()+pl.getBall().getRadius()+1)));
			this.setMotion(new Vector(0,0));
			
			if(throwable()){
				Level.getInstance().registerObject(this);
				this.used = true;
				this.pl.setWeaponState();
			} else {
				System.out.println("abwurf nicht moegl.");
			}
		}
	}
	public boolean newMotion(WorldObject object){
		boolean ret = true;
		double dist; 
	//		Ball obj = (Ball) object;
		for(int i = 0; i < Level.getInstance().getPlayers().size();i++){
			Ball b = Level.getInstance().getPlayers().get(i).getBall();
			if(b.isVisible() && (dist=this.getPosition().getDist(b.getPosition()))<DEST_RAD){
				/*if(Level.getInstance().getPlayers().get(i).getBall()==object){
						obj.setPosition(obj.getPosition().add(obj.getMotion()));
					}*/
				if((int)(Level.getInstance().getPlayers().get(i).getHP()-(DEST_RAD-dist+20))>0){
					int destr = (int)(DEST_RAD-dist+15);
					this.pl.addDamage(destr);
					Level.getInstance().getPlayers().get(i).setHP(Level.getInstance().getPlayers().get(i).getHP()-destr);
					Vector tmp = Level.getInstance().getPlayers().get(i).getBall().getPosition().sub(this.getPosition());
					Level.getInstance().getPlayers().get(i).getBall().setMotion(tmp.setLength(5));
				} else {
					if(Level.getInstance().getPlayers().get(i).getBall()==object){
						ret = false;
					}
					int destr = (int)(DEST_RAD-dist+15);
					Level.getInstance().getPlayers().get(i).setHP(Level.getInstance().getPlayers().get(i).getHP()-destr);
					this.pl.addDamage(destr);
				}
			}
		}
		Level.getInstance().removeObject(this);

		//load animation of explosion

		explosion.startAt(new Point(this.getPosition().getIntX()-27, this.getPosition().getIntY()-55), false);
		this.setCollitabe(false);

		Level.getInstance().registerGround(new Pit(this.getPosition().getCopy(),15, 0.001));
		return ret;
	}
	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	public String getType(){
		return WeaponType;
	}
	public int getPrice() {
		return Price;
	}
	public Player getPlayer(){
		return this.pl;
	}
	public void remove(){
		pl.removeWeapon(this);
		this.pl.setAktWeapon(null);
	}
	public double getState(){
		if(this.used){
			return 0;
		}else{
			return 1;
		}
	}
	private boolean throwable(){
		for(int i = 0; i < Level.getInstance().getObjects().size();i++){
			WorldObject obj = Level.getInstance().getObjects().get(i);
			if(obj != pl.getBall() && this.kollision(obj)==0 ){
				return false;
			}
		}
		for(int i = 0; i< Level.getInstance().getHoles().size();i++){
			Hole obj = Level.getInstance().getHoles().get(i);
			if(this.kollision(obj)==0 ){
				return false;
			}
		}
		return true;
	} 
}
